package com.yiren.dbaa.module.exam.api.impl;

import android.app.Activity;
import android.os.Handler;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.constant.AddressConstant;
import com.yiren.dbaa.module.exam.api.IExamStudentApi;
import com.yiren.dbaa.net.OkHttpUtils;
import com.yiren.dbaa.net.bean.BaseResponseBean;
import com.yiren.dbaa.net.bean.QrCodeBean;
import com.yiren.dbaa.net.bean.StudentBean;
import com.yiren.dbaa.net.interf.StringCallback;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Describe
 *
 * @author WRX
 * @date 2022/7/4
 */
public class ExamStudentApiImpl implements IExamStudentApi {

    private Activity activity;
    private Handler timeHandler;

    public ExamStudentApiImpl(Activity activity) {
        this.activity = activity;
        timeHandler = new Handler();
    }

    @Override
    public void getStuQrCodeUrl(String model, CommonCallBack callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("model", (null == model ? "" : model));
        OkHttpUtils.getInstance().postMapByAsync(activity, AddressConstant.getQrCodeUrl(), map, new StringCallback() {
            @Override
            public void getString(String result) {
                BaseResponseBean<QrCodeBean> responseBean = new Gson().fromJson(result, new TypeToken<BaseResponseBean<QrCodeBean>>() {
                }.getType());
                if (responseBean.getError() == null) {
                    QrCodeBean qrCodeBean = responseBean.getResult();
                    callBack.success(qrCodeBean.getRedirectUrl());
                } else {
                    callBack.fail(responseBean.getError().getMessage());
                }
                callBack.complete();
            }

            @Override
            public void getFail() {
                callBack.complete();
            }
        });
    }

    @Override
    public void checkStuSweepCode(CommonCallBack callBack) {
        if (activity.isFinishing()) {
            return;
        }
        OkHttpUtils.getInstance().postJsonString(activity, AddressConstant.isHaveScanQrCodeUrl(""), "", new StringCallback() {
            @Override
            public void getString(String result) {
                BaseResponseBean<StudentBean> responseBean = new Gson().fromJson(result, new TypeToken<BaseResponseBean<StudentBean>>() {
                }.getType());
                if (responseBean.getError() == null) {
                    StudentBean studentBean = responseBean.getResult();
                    if (null != studentBean) {
                        callBack.success(studentBean);
                    } else {
                        timeHandler.postDelayed(() -> checkStuSweepCode(callBack), 1000);
                    }
                } else {
                    timeHandler.postDelayed(() -> checkStuSweepCode(callBack), 1000);
                }
            }

            @Override
            public void getFail() {
                timeHandler.postDelayed(() -> checkStuSweepCode(callBack), 1000);
            }
        });
    }

    @Override
    public void removeCallback() {
        timeHandler.removeCallbacksAndMessages(null);
    }

    @Override
    public void requestStudentList(CommonCallBack callBack) {
        OkHttpUtils.getInstance().getStringByAsync(activity, AddressConstant.getLoadStudentAuthsUrl(), new StringCallback() {
            @Override
            public void getString(String result) {
                BaseResponseBean<List<StudentBean>> responseBean = new Gson().fromJson(result, new TypeToken<BaseResponseBean<List<StudentBean>>>() {
                }.getType());
                if (responseBean.getError() == null) {
                    callBack.success(responseBean.getResult());
                } else {
                    callBack.fail(responseBean.getError().getMessage());
                }
            }
        });
    }

}
